/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DccShangpin;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DccShangpinRepository;
import me.zhengjie.modules.system.service.DccShangpinService;
import me.zhengjie.modules.system.service.dto.DccShangpinDto;
import me.zhengjie.modules.system.service.dto.DccShangpinQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DccShangpinMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Cjl
* @date 2023-05-19
**/
@Service
@RequiredArgsConstructor
public class DccShangpinServiceImpl implements DccShangpinService {

    private final DccShangpinRepository dccShangpinRepository;
    private final DccShangpinMapper dccShangpinMapper;

    @Override
    public Map<String,Object> queryAll(DccShangpinQueryCriteria criteria, Pageable pageable){
        Page<DccShangpin> page = dccShangpinRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dccShangpinMapper::toDto));
    }

    @Override
    public List<DccShangpinDto> queryAll(DccShangpinQueryCriteria criteria){
        return dccShangpinMapper.toDto(dccShangpinRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DccShangpinDto findById(Integer id) {
        DccShangpin dccShangpin = dccShangpinRepository.findById(id).orElseGet(DccShangpin::new);
        ValidationUtil.isNull(dccShangpin.getId(),"DccShangpin","id",id);
        return dccShangpinMapper.toDto(dccShangpin);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DccShangpinDto create(DccShangpin resources) {
        return dccShangpinMapper.toDto(dccShangpinRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DccShangpin resources) {
        DccShangpin dccShangpin = dccShangpinRepository.findById(resources.getId()).orElseGet(DccShangpin::new);
        ValidationUtil.isNull( dccShangpin.getId(),"DccShangpin","id",resources.getId());
        dccShangpin.copy(resources);
        dccShangpinRepository.save(dccShangpin);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dccShangpinRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DccShangpinDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DccShangpinDto dccShangpin : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("状态0下架 1上架", dccShangpin.getStatus());
            map.put("线索分类", dccShangpin.getType());
            map.put("所需积分", dccShangpin.getJifen());
            map.put("总数", dccShangpin.getTotal());
            map.put("商品名称", dccShangpin.getDccTitle());
            map.put("商品简介", dccShangpin.getDccText());
            map.put("图片列表", dccShangpin.getDccImglist());
            map.put("视频列表", dccShangpin.getDccVediolist());
            map.put("联系方式", dccShangpin.getContract());
            map.put("操作者", dccShangpin.getOperator());
            map.put(" createdAt",  dccShangpin.getCreatedAt());
            map.put(" updatedAt",  dccShangpin.getUpdatedAt());
            map.put("外链", dccShangpin.getOutUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}